package com.gzhryc.log.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.gzhryc.common.DateTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.Conditions.Criterion;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.log.dao.HttpVisitRecordDao;
import com.gzhryc.log.dao.ManagerVisitRecordDao;
import com.gzhryc.log.dao.db.HttpVisitRecord;
import com.gzhryc.log.dao.models.HttpVisitCountDay;
import com.gzhryc.log.services.dto.HttpVisitRecordSearch;

/**
 * HTTP访问记录服务
 * @author 张浩
 * @version 2017-1-11
 */
public class HttpVisitRecordService {

	public HttpVisitRecordService self() {
		return MultiDBTools.getService(HttpVisitRecordService.class, true);
	}

	public HttpVisitRecordService self(String jdbcKey) {
		return MultiDBTools.getService(jdbcKey,HttpVisitRecordService.class, true);
	}

	HttpVisitRecordDao dao;

	public HttpVisitRecordService(String jdbcKey) {
		this.dao = new HttpVisitRecordDao(jdbcKey);
	}
	
	/**
	 * 分页查询所有访问记录
	 * @param search
	 * @param pageNo
	 * @param pageLimit
	 * @return
	 */
	public List<HttpVisitRecord> findBySearch(HttpVisitRecordSearch search,Integer pageNo,Integer pageLimit){
		Conditions condition = new Conditions(HttpVisitRecord.class);
		condition.and().is("path", search.getPath());
		condition.and().is("clientType", search.getClientType());
		condition.and().ge("createDate", search.getStartDate());
		condition.and().le("createDate", search.getEndDate());
		condition.desc("createDate");
		try {
			return dao.findByCondition(condition, pageNo, pageLimit);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 统计访问数量
	 * @param search
	 * @return
	 */
	public Long countBySearch(HttpVisitRecordSearch search){
		Conditions condition = new Conditions(HttpVisitRecord.class);
		condition.and().is("path", search.getPath());
		condition.and().is("clientType", search.getClientType());
		condition.and().ge("createDate", search.getStartDate());
		condition.and().le("createDate", search.getEndDate());
		try {
			return dao.count(condition);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0L;
	}
	
	public List<HttpVisitCountDay> countGroupDay(Integer year,Integer month){
		Conditions condition = new Conditions(HttpVisitRecord.class);
		condition.and().is("year", year);
		condition.and().is("month", month);
		try {
			return dao.countGroupDay(condition);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public List<HttpVisitCountDay> countIpGroupDay(Integer year,Integer month){
		Conditions condition = new Conditions(HttpVisitRecord.class);
		condition.and().is("year", year);
		condition.and().is("month", month);
		try {
			return dao.countDistinctIpGroupDay(condition);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 统计某天某些路径访问数
	 * @param date
	 * @param paths
	 * @return
	 */
	public Long countByDay(Date date,Integer clientType,String ... paths){
		Conditions condition = new Conditions(HttpVisitRecord.class);
		if(paths != null){
			Criterion criterion = condition.and();
			for(int i=0;i<paths.length;i++){
				criterion = criterion.is("path",paths[i]).or();
			}
		}
		Date startDate = DateTools.getDayStart(date);
		Date endDate = DateTools.getDayEnd(date);
		condition.and().is("clientType", clientType);
		condition.and().ge("createDate", startDate);
		condition.and().le("createDate", endDate);
		try {
			return dao.count(condition);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0L;
	}
	
	public Long countIpByDay(Date date,Integer clientType,String ... paths){
		Conditions condition = new Conditions(HttpVisitRecord.class);
		if(paths != null){
			Criterion criterion = condition.and();
			for(int i=0;i<paths.length;i++){
				criterion = criterion.is("path",paths[i]).or();
			}
		}
		Date startDate = DateTools.getDayStart(date);
		Date endDate = DateTools.getDayEnd(date);
		condition.and().is("clientType", clientType);
		condition.and().ge("createDate", startDate);
		condition.and().le("createDate", endDate);
		try {
			return dao.countDistinctIpByCondition(condition);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0L;
	}
	
	/**
	 * 根据ID获取访问记录
	 * @param id
	 * @return
	 */
	public HttpVisitRecord getById(Long id){
		try {
			return dao.getById(id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 根据ID删除访问记录
	 * @param id
	 * @return
	 */
	public boolean deleteById(Long id){
		if(id != null){
			try {
				return dao.deleteById(id) > 0;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return false;
	}
	
	/**
	 * 新增访问记录
	 * @param httpLink
	 * @return
	 * @throws LogicException
	 */
	public boolean addHttpLink(HttpVisitRecord httpLink) {
		try {
			return dao.insert(httpLink) > 0;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 修改新增记录 
	 * @param httpLink
	 * @return
	 */
	public synchronized boolean updateHttpLink(HttpVisitRecord httpLink){
		try {
			return dao.updateNotNull(httpLink) > 0;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
}
